package com.mmd.openfire_admin.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mmd.openfire_admin.constant.Constant;
import com.mmd.openfire_admin.constant.ErrorCode;
import com.mmd.openfire_admin.generator.pojo.OfuserinfoExample;
import com.mmd.openfire_admin.pojo.ResponseMsg;
import com.mmd.openfire_admin.service.RedisService;
import com.mmd.openfire_admin.service.UserInfoService;
import com.mmd.openfire_admin.util.FastjsonUtil;
import com.mmd.openfire_admin.util.RequestRenderUtil;
import com.mmd.openfire_admin.util.RequestUtil;
import com.mmd.openfire_admin.util.SpringUtil;
import com.mmd.openfire_admin.util.StringUtil;



/**
 * 通行证验证过滤器
 * 
 */
public class uploadValidateFilter implements javax.servlet.Filter {

	protected Logger log = LoggerFactory.getLogger(getClass());
	
//	private RedisService redisService;
	private UserInfoService userInfoService;



	public void init(FilterConfig filterConfig) throws ServletException {
//		redisService = SpringUtil.getBean(RedisService.class);
		userInfoService = SpringUtil.getBean(UserInfoService.class);
	}

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		
		ResponseMsg retMsg = null;
		
		HttpServletRequest rq = (HttpServletRequest) request;
		HttpServletResponse rs = (HttpServletResponse) response;
		
		String username = RequestUtil.get(rq, "username");
		String token = RequestUtil.get(rq, "token");
		
		log.debug("URI:" + rq.getRequestURI());
		log.debug("url:" + rq.getRequestURL().toString());
		log.debug("username:" + username);
		log.debug("token:" + token);
		
		if(StringUtil.isBlank(username) || StringUtil.isBlank(token)){
			retMsg = new ResponseMsg("缺少必要参数！", ErrorCode.ERROR, null);
			RequestRenderUtil.renderText(rs,FastjsonUtil.toJSONString(retMsg), "utf-8");
			return;
		}
		
//		String token_in = redisService.get(Constant.USER_TOKEN_PRE + username);
//		if(token.equals(token_in)){
//			chain.doFilter(request, response);
//		}else{
		System.out.println("uploadValidateFilter --- username --- " + username);
		System.out.println("uploadValidateFilter --- token --- " + token);
			OfuserinfoExample example = new OfuserinfoExample();
			example.or().andUsernameEqualTo(username).andUuidEqualTo(token);
			int count = userInfoService.countByExample(example);
			System.out.println("uploadValidateFilter --- count --- " + count);
			if(count > 0){
				chain.doFilter(request, response);
			}else{   
				retMsg = new ResponseMsg("登录验证失败！", ErrorCode.INVALID_SID, null);
				RequestRenderUtil.renderText(rs,FastjsonUtil.toJSONString(retMsg), "utf-8");
				return;
			}
//		}
	}

	public void destroy() {

	}
}
